/*
 * @lc app=leetcode.cn id=257 lang=typescript
 *
 * [257] 二叉树的所有路径
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * class TreeNode {
 *     val: number
 *     left: TreeNode | null
 *     right: TreeNode | null
 *     constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
 *         this.val = (val===undefined ? 0 : val)
 *         this.left = (left===undefined ? null : left)
 *         this.right = (right===undefined ? null : right)
 *     }
 * }
 */

function binaryTreePaths(root: TreeNode | null): string[] {
    return _binaryTreePaths(root).map((i) => {
        return i.join("->");
    });
}
// 主要是递归返回 父级 + 子级 遍历结果
// 从头开始累计的话，就会形成一条线路了
function _binaryTreePaths(root: TreeNode | null): number[][] {
    if (!root) return [];
    if (!root.left && !root.right) return [[root.val]];
    return [
        ..._binaryTreePaths(root.left).map((i) => [root.val, ...i]),
        ..._binaryTreePaths(root.right).map((i) => [root.val, ...i]),
    ];
}
// @lc code=end
